<html xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"  
      xmlns:c="http://java.sun.com/jsp/jstl/core">  

    <ui:composition template="layout.xhtml">
        <ui:define name="corpo">
            
<!--            <h1 style="font-size: 18px;">Atendimento Inicial</h1>-->
            <h:form>
                <div id="breadcrumb">
                    <p:commandLink  actionListener="#{managerIndex.redirect('index.xhtml')}" value="Inicio" update="@form" immediate="true"/> >
                    <p:commandLink  actionListener="#{managerIndex.redirect('indexProtocolo.xhtml')}" value="Protocolo" update="@form" immediate="true"/> >
                    <p:commandLink   value="Orçamento" update="@form" immediate="true"/>
                </div>
            </h:form>
           <p:tabView dynamic="true" id="tabView" activeIndex="#{managerOrcamento.tab}">  
                <p:tab id="tab1" title="Atendimento">  
                
                    <h:form onreset="#{managerOrcamento.limpar()}" id="orcamento" acceptcharset="utf-8"> 
                        
                        <p:messages id="msg"/>
                        <div style="text-align: right;">
                            <h:outputLabel value="Orçamento ##{managerOrcamento.orcamento.id}" rendered="#{managerOrcamento.orcamento.id eq 0 ? false : true}" />
                        </div>
                           
                        <fieldset>
                            <legend>Dados</legend> 
                            <h:panelGrid columns="5" id="panel"  styleClass="textAlignRight" columnClasses="textAlignRight"> 
                                <h:outputLabel value="Doc.: "/>
                                <p:inputText value="#{managerOrcamento.orcamento.doc}" id="pessoacpf" style="width: 175px;" required="true" requiredMessage="#{vm['orcamento.doc']}" >
                                    <p:ajax event="blur" update="@this" process="@this"/>
                                    <p:ajax event="change" update="@this" />
                                </p:inputText>
                                
                                
                                <p:commandButton icon="ui-icon-search" actionListener="#{managerOrcamento.buscarPessoa()}" update="msg, @this, pessoacpf, interessado " process="@this, pessoacpf" style="margin-left: -5px;heigth:10px; width: 30px"/>
                                <h:outputLabel value="Interessado: "/>
                                <p:inputText id="interessado"  value="#{managerOrcamento.orcamento.nome}" 
                                             style="width: 570px;" 
                                             required="true" requiredMessage="Campo Interessado é obrigatório"/>

                            </h:panelGrid>
                        </fieldset>
                        <fieldset>
                            <legend>Serviço</legend> 
                            <p:focus for="atoItem" />
                            <h:panelGrid columns="2" id="panelServico"  styleClass="textAlignRight" columnClasses="textAlignRight"> 
                                <h:outputLabel  value="Ato:" />
                                <p:autoComplete id="atoItem" effect="blind" value="#{managerOrcamento.orcamentoAto.atoItem}" 
                                                scrollHeight="200" completeMethod="#{managerAto.completeAtoItem}"  
                                                size="106" var="p" itemLabel="#{p.sigla.concat(' - ')}#{p.descricao}" 
                                                itemValue="#{p}" converter="atoItemConverter"  
                                                converterMessage="Erro, verifique o Ato"  forceSelection="true"
                                                dropdown="true" required="true" requiredMessage="O campo 'Ato' é obrigatório"> 
                                    <p:ajax event="itemSelect" listener="#{managerOrcamento.gerarCampos}" update="panelCampos"/>                                        

                                </p:autoComplete>
                            </h:panelGrid>

                            <h:panelGrid columns="2"  width="950" id="panelCampos" style="margin-top: 5px;" > 
                                <ui:repeat id="rptCampos" value="#{managerOrcamento.orcamentoCampos}" 
                                           rendered="#{! empty managerOrcamento.orcamentoCampos and managerOrcamento.orcamentoAto.atoItem != null}" 
                                           var="orc" >
                                    <p:row>                                          
                                        <h:outputLabel  value="#{orc.atoCampo.nome}: " /> 
                                        <p:inputText  value="#{orc.valor}" rendered="#{orc.atoCampo.tipo.equalsIgnoreCase('Integer')}" onkeypress="jQuery(this).autoNumeric({aSep: '.', aDec: ','} );" required="true" requiredMessage="Campo obrigatório"/>
                                        <p:inputText  value="#{orc.valor}" rendered="#{orc.atoCampo.tipo.equalsIgnoreCase('BigDecimal')}" onkeypress="jQuery(this).autoNumeric({aSep: '.', aDec: ','} );" required="true" requiredMessage="Campo obrigatório"/>
                                        <p:inputText  value="#{orc.valor}" rendered="#{orc.atoCampo.tipo.equalsIgnoreCase('String')}" required="true" requiredMessage="Campo obrigatório"/> 

                                    </p:row> 
                                </ui:repeat>

                            </h:panelGrid>
                            <div class="buttonAction">
                                
                                <p:commandButton icon="ui-icon-circle-plus" value="Adicionar" 
                                                 actionListener="#{managerOrcamento.addServico()}"
                                                 update="@this, atoItemTable, panelCampos, panelServico, panelObs, msg"
                                                 process="@this, panelCampos, panelServico"
                                                 
                                                 style="float: right;"/>
                                
                                <p:commandButton icon="ui-icon-circle-close" value="Limpar" 
                                                 actionListener="#{managerOrcamento.limpar()}" 
                                                 update="panelServico, panelCampos" process="@this" 
                                                 style="float: right;"/>
                                
                            </div>   

                        
                        <h:panelGrid id="dataTableAtoItem">
                                <p:dataTable var="ato" id="atoItemTable" 
                                            value="#{managerOrcamento.orcamentoAtos}"
                                            rowIndexVar="rowid" style="width:940px;"  
                                            rowKey="#{ato.id}" selectionMode="single" 
                                            emptyMessage="Nenhum Serviço Cadastrado">  
                                    <f:facet name="header">
                                        Serviços
                                    </f:facet>
                                    <p:column headerText="" style="width: 65px">  
                                        <p:commandButton  style="width: 20px; height: 20px;" onclick="editeOrcamentoAto({paramOrcAto:#{rowid}})" icon="ui-icon-pencil" process="@this" title="Editar" />
                                        <p:commandButton  style="width: 20px; height: 20px;" onclick="showOrcamentoAto({paramShowOrcAto:#{rowid}})" icon="ui-icon-search" process="@this" title="Visualizar"/>
                                        <p:commandButton  style="width: 20px; height: 20px;" onclick="removeOrcamentoAto({paramRemoverOrcAto:#{rowid}})" icon="ui-icon-trash" process="@this" update="@this" immediate="true" title="Remover"/>
                                        
                                    </p:column>
                                    <p:column headerText="Ato"  >  
                                        <h:outputText value="#{ato.atoItem.codigo} - #{ato.atoItem.descricao}" />  
                                    </p:column>
                                    <p:column headerText="Prazo" style="width:100px">  
                                        <h:outputText value="#{ato.atoItem.prazo}" style="float: right;" />  
                                    </p:column>
                                    <p:column headerText="Valor R$"  style="width:128px;">  
                                        <h:outputText value="#{ato.valor}" styleClass="teste" style="float: right;" >
                                            <f:convertNumber pattern="#,###,##0.00"/>
                                        </h:outputText>
                                    </p:column>
                                </p:dataTable> 
                                <p:remoteCommand name="editeOrcamentoAto"
                                                 update="msg, atoItemTable, panelServico, panelCampos" process="@this" immediate="true" actionListener="#{managerOrcamento.editarList()}">
                                </p:remoteCommand>
                                <p:remoteCommand name="removeOrcamentoAto"
                                                 update="atoItemTable" process="@this" actionListener="#{managerOrcamento.remover()}">
                                </p:remoteCommand>
                                <p:remoteCommand name="showOrcamentoAto"
                                                 update="msg, atoItemTable, servico, panelCamposVisualizar" immediate="true" process="@this" oncomplete="servicoAto.show()" actionListener="#{managerOrcamento.dialogShow()}">
                                </p:remoteCommand>
                          </h:panelGrid>
                         </fieldset>
                        
                        <h:outputLabel value="Observação: "/>
                        <h:panelGrid columns="2" id="panelObs" styleClass="textAlignRight" columnClasses="textAlignRight">                                 
                            <p:inputTextarea value="#{managerOrcamento.orcamento.observacao}" cols="85" maxlength="500"
                                             style="width: 689px;" rows="5" />       

                            <h:panelGrid columns="2"  styleClass="textAlignRight" columnClasses="textAlignRight" style="float:right;margin-top: 10px; margin-right: 35px;">
                                <h:outputLabel value="Total: "/>
                                <p:row>
                                    <p:inputText id="total"  style="width: 183px; text-align: right"/>
                                    <p:watermark for="total"  value="#{managerOrcamento.somaServicos()}" />
                                </p:row>
                                <h:outputLabel value="Prazo: " />
                                <p:row>
                                    <p:inputText id="prazo" value="#{managerOrcamento.prazo}" style="width: 183px; text-align: right"/>
                                    <p:watermark for="prazo" value="#{managerOrcamento.prazo()}" />
                                </p:row>
                                <h:outputLabel value="Validade: "/>   
                                <p:row>
                                    <p:calendar id="prefix" value="#{managerOrcamento.orcamento.validade}"
                                                mode="popup" pattern="dd/MM/yyyy" showOn="button"
                                                locale="pt" showButtonPanel="true" navigator="true" 
                                                style="width: 30px" size="17"/>
                                    <p:watermark for="prefix" value="#{managerOrcamento.validade()}" />
                                </p:row>
                            </h:panelGrid>
                        </h:panelGrid>
                        <br/>
                        <div class="buttonAction">
                            <p:commandButton icon="ui-icon-plusthick" value="Novo" actionListener="#{managerOrcamento.limparAll()}" update="@form" process="@this"/>
                            <p:commandButton icon="ui-icon-disk" value="#{managerOrcamento.orcamento.id eq 0 ?'Salvar' : 'Atualizar'}" actionListener="#{managerOrcamento.salvar()}" update="@form" process="@this, panel, panelObs"/>
                            <p:commandButton icon="ui-icon-print" value="Imprimir" 
                                             action="#{managerOrcamento.gerarRelatorioOrcamento(managerOrcamento.orcamento.id)}"
                                             disabled="#{managerOrcamento.orcamento.id eq 0 ? true : false}"
                                             onclick="this.form.target='_blank'"
                                             ajax="false" immediate="true" update="@form" process="@this"/>
                            <p:commandButton icon="ui-icon-document-b" value="Protocolar" 
                                             actionListener="#{managerProtocolo.recept(managerOrcamento.orcamentoToProtocolo())}"
                                             disabled="#{managerOrcamento.orcamento.id eq 0 ? true : false}" 
                                             update="@form" process="@form" immediate="true"/>
                            
                             <br/>
                            <center>
                                <p:ajaxStatus style="width:16px;height:16px;" >  
                                    <f:facet name="start">  
                                        <h:graphicImage value="/media/img/loading.gif" />  
                                    </f:facet>  
                                    <f:facet name="complete">  
                                        <h:outputText value="" />  
                                    </f:facet>  
                                </p:ajaxStatus>
                            </center>
                         </div>
                        
                        
                         <p:dialog id="servicoAto" modal="true" header="Serviço" widgetVar="servicoAto" resizable="false"  
                          width="450" minHeight="400" showEffect="slide" hideEffect="fold" >
                            <p:ajax event="close" listener="#{managerOrcamento.dialogClose()}" process="@this" update="servico, panelCamposVisualizar, rptCamposVisualizar"/>
                            <h:panelGrid id="servico"   columns="2" styleClass="textAlignRight" columnClasses="textAlignRight">
                                
                                <h:outputLabel value="Cód.: " style="font-weight: bold;"/>
                                <h:outputText value="#{managerOrcamento.orcamentoAtosSelecionado.atoItem.codigo}"/>
                                
                                <h:outputLabel value="Ato: " style="font-weight: bold;"/>
                                <h:outputText value="#{managerOrcamento.orcamentoAtosSelecionado.atoItem.descricao}"/>
                               
                               
                            </h:panelGrid>
                             <h:panelGrid columns="1"  id="panelCamposVisualizar" > 
                                 <table >
                                            <ui:repeat id="rptCamposVisualizar" value="#{managerOrcamento.orcamentoCamposVisualizar}" 
                                                    rendered="#{! empty managerOrcamento.orcamentoCamposVisualizar}" 
                                                    var="orcVer" >
                                                <tr >
                                                <td class="textAlignRight" style="width:29px; margin-left: 0px" >
                                                    <h:outputLabel  value="#{orcVer.atoCampo.nome}: " style="font-weight: bold;"/> 
                                                </td>
                                                <td>
                                                    <h:outputText  value="#{orcVer.valor}" rendered="#{orcVer.atoCampo.tipo.equalsIgnoreCase('Integer')}" />
                                                    <h:outputText  value="#{orcVer.valor}" rendered="#{orcVer.atoCampo.tipo.equalsIgnoreCase('BigDecimal')}"  />
                                                    <h:outputText  value="#{orcVer.valor}" rendered="#{orcVer.atoCampo.tipo.equalsIgnoreCase('String')}" /> 
                                                </td>
                                            </tr>
                                        </ui:repeat>
                                 </table>
                            </h:panelGrid>
                        </p:dialog>

                    </h:form>

                </p:tab>
                <p:tab title="Pesquisar">
                      <h:form acceptcharset="utf-8"  prependId="false"> 
                       <p:messages id="msgPesquisar"/>
                        <fieldset>
                            <legend>Pesquisar</legend> 
                            <f:validateBean disabled="true">
                            <h:panelGrid columns="2" id="panelPesquisa"  styleClass="textAlignRight" columnClasses="textAlignRight"> 
                                
                                <h:outputLabel value="Código: "/>
                                <p:inputText value="#{managerConsultarOrcamento.cod}" style="width: 172px;"/>
                                
                                <h:outputLabel value="Data: "/>
                                <p:calendar  value="#{managerConsultarOrcamento.orcamento.dataCadastro}" 
                                             mode="popup" pattern="dd/MM/yyyy" showOn="both" 
                                             showButtonPanel="true" navigator="true" size="16"
                                             locale="pt"/>
                                
                                <h:outputLabel value="Documento: "/>
                                <p:inputText value="#{managerConsultarOrcamento.orcamento.doc}" style="width: 172px;" >
                                    <f:validateBean disabled="true"/>
                                <p:ajax event="blur" update="@this" process="@this"/>
                                    <p:ajax event="change" update="@this" />
                                </p:inputText>
                                
                                <h:outputLabel value="Interessado: "/>
                               <p:inputText  value="#{managerConsultarOrcamento.orcamento.nome}" style="width: 570px;">
                                   <f:validateBean disabled="true"/>
                               </p:inputText>

                                
                                
                            </h:panelGrid>
                                </f:validateBean>
                            <br/>
                            <div class="buttonAction">
                                <p:commandButton icon="ui-icon-search" value="Pesquisar" actionListener="#{managerConsultarOrcamento.buscar()}" update="@form" />
                                <p:commandButton icon="ui-icon-close" value="Limpar" action="#{managerConsultarOrcamento.reinit()}" update="@form" process="@this"/>
                                  
                                    <center>
                                        <p:ajaxStatus style="width:16px;height:16px;" id="carregandoBuscar">  
                                            <f:facet name="start">  
                                                <h:graphicImage value="/media/img/loading.gif" />  
                                            </f:facet>  
                                            <f:facet name="complete">  
                                                <h:outputText value="" />  
                                            </f:facet>  
                                        </p:ajaxStatus>
                                    </center>
                            </div>
                            <p:dataTable var="orc" id="dataTablePesquisar"
                                         value="#{managerConsultarOrcamento.lstOrcamento}"
                                         paginator="true"
                                         paginatorPosition="bottom" rows="20"
                                        rowIndexVar="rowid" style="width:940px;" 
                                        rowKey="#{orc.id}" selectionMode="single" 
                                        emptyMessage="Nenhum Orçamento Encontrado">  
                            <f:facet name="header">
                                Orçamentos
                            </f:facet>
                            <p:column headerText="" style="width: 45px">  
                                <p:commandButton  style="width: 20px; height: 20px;" 
                                                  actionListener="#{managerOrcamento.resgatar(orc.id)}" 
                                                  icon="ui-icon-arrowreturnthick-1-w" process="@this" 
                                                  update="@form, :tabView, :formcorpo" title="Resgatar"/>
                                 <p:commandButton  style="width: 20px; height: 20px;" 
                                                   onclick="showOrcamentoAtoPesquisar({paramShowOrcAtoPesquisar:#{rowid}})"
                                                   icon="ui-icon-search" process="@this" title="Visualizar"/>
                            </p:column>
                            <p:column headerText="Data" style="width:80px;" >  
                                <h:outputText value="#{orc.dataCadastro}" >
                                    <f:convertDateTime pattern="dd/MM/yyyy"/>
                                </h:outputText>
                            </p:column>
                            <p:column headerText="Interessado"  >  
                                <h:outputText value="#{orc.nome}" />  
                            </p:column>
                            <p:column headerText="DOC" style="width:150px;">  
                                <h:outputText value="#{orc.doc}" style="float: left;" />  
                            </p:column>
                             <p:column headerText="Obs." style="width:250px;" >  
                                 <h:outputText value="#{orc.observacao}" style="float: left;" />  
                            </p:column>
                        </p:dataTable>
                            
                        <p:remoteCommand name="showOrcamentoAtoPesquisar"
                                        update="dataTablePesquisar, servicoPesquisar, panelServicoVisualizar" 
                                        immediate="true" process="@this" oncomplete="servicoAtoPesquisar.show()" 
                                        actionListener="#{managerConsultarOrcamento.dialogShow()}">
                        </p:remoteCommand>
                            
                        </fieldset>
                       
                       <p:dialog id="servicoAtoPesquisar" modal="true" header="Resumo Orçamento" widgetVar="servicoAtoPesquisar" resizable="false"  
                                 width="450" minHeight="400" style="max-height: 800px;"  showEffect="slide" hideEffect="fold" >
                            <p:ajax event="close" listener="#{managerOrcamento.dialogClose()}" process="@this" update="dataTablePesquisar"/>
                            
                            <h:panelGrid id="servicoPesquisar"  columns="2" styleClass="textAlignRight" columnClasses="textAlignRight">
                                <h:outputLabel value="Cód.: " style="font-weight: bold;"/>
                                <h:outputText value="#{managerConsultarOrcamento.orcamentoVisualizar.id}"/>
                                <h:outputLabel value="Interessado: " style="font-weight: bold;"/>
                                <h:outputText value="#{managerConsultarOrcamento.orcamentoVisualizar.nome}"/>
                                <h:outputLabel value="Documento: " style="font-weight: bold;"/>
                                <h:outputText value="#{managerConsultarOrcamento.orcamentoVisualizar.doc}"/>
                                <h:outputLabel value="Data Orc.: " style="font-weight: bold; "/>
                                <p:row>
                                        <h:outputText value="#{managerConsultarOrcamento.orcamentoVisualizar.dataCadastro}">
                                            <f:convertDateTime pattern="dd/MM/yyyy"/>
                                        </h:outputText>
                                    <h:outputText value="Validade: " style="margin-left: 20px; font-weight: bold; "/>
                                    <h:outputText value="#{managerConsultarOrcamento.orcamentoVisualizar.validade eq null ? ' - ' : managerConsultarOrcamento.orcamentoVisualizar.validade }">
                                        <f:convertDateTime pattern="dd/MM/yyyy" rendered="#{managerConsultarOrcamento.orcamentoVisualizar.validade eq null ? false : true }"/>
                                        </h:outputText>
                                </p:row>
                            </h:panelGrid>
                            <br/>
                             <h:panelGrid columns="1"  id="panelServicoVisualizar" > 
                                 <table  style="width: 427px; ">
                                     <thead>
                                         <tr>
                                             <th style="background-color: #CAD8DD;  font-weight: bold; text-align: center" rowspan="3" colspan="3">
                                                 Serviços
                                             </th>    
                                         </tr>
                                     </thead>
                                     <tbody style="background-color: #5277A9; color: #ffffff; ">
                                     
                                     <td style="font-weight: bold;">
                                         Cód
                                     </td>
                                     <td style="font-weight: bold;">
                                         Ato
                                    </td>
                                     <td style="font-weight: bold; text-align: center;">
                                         Valor
                                     </td>
                                    
                                 </tbody>
                                     <ui:repeat value="#{managerConsultarOrcamento.orcamentoVisualizar.orcamentoAtos}" 
                                                var="orcAtoVer" varStatus="teste" >
                                         <tr style="#{teste.index mod 2 eq 0 ? '':'background-color: #F5F5F5'}">
                                                    <td class="textAlignRight" style="border-top: 1px solid #CAD8DD;" >
                                                        <h:outputLabel  value="#{orcAtoVer.atoItem.codigo}" /> 
                                                    </td>
                                                    <td class="textAlignRight" style="border-top: 1px solid #CAD8DD;" >
                                                        <h:outputText  value="#{orcAtoVer.atoItem.descricao}" />
                                                    </td>
                                                    <td class="textAlignRight" style="border-left: 1px solid #CAD8DD; border-top: 1px solid #CAD8DD;" >
                                                        <h:outputText  value="#{orcAtoVer.valor}" >
                                                            <f:convertNumber pattern="#,###,##0.00"/>
                                                        </h:outputText>
                                                    </td>
                                                </tr>
                                        </ui:repeat>
                                     <p:row>
                                         <tr style="border-top: 1px; margin-top: 5px;"   >
                                             <td colspan="2" >
                                                 <h:outputLabel style="float:right;" value="Total R$"/>
                                            </td>
                                            <td>            
                                                <h:outputLabel value="#{managerConsultarOrcamento.somaServicos()}" style="font-weight: bold; "/>
                                            </td>
                                     </tr>
                                     </p:row>
                                 
                                 </table>
                                 <br/>
                                 <f:facet name="footer">
                                     <h:outputLabel value="Obs.: " style="font-weight: bold;"/>
                                    <h:outputLabel value="#{managerConsultarOrcamento.orcamentoVisualizar.observacao}"/> 
                                 </f:facet>
                            </h:panelGrid>
                            
                            
                            <f:facet name="footer" >
                                <div style="text-align: center;">
                                    <p:commandButton  style="font-size:10px; width: 80px; height: 25px;" 
                                                      action="#{managerOrcamento.resgatar(managerConsultarOrcamento.orcamentoVisualizar.id)}" 
                                                      icon="ui-icon-arrowreturnthick-1-w" process="@this" 
                                                      onclick="servicoAtoPesquisar.hide()" update="@this, @form, :tabView" value="Resgatar"/>
                                    
                                    <p:commandButton  style="font-size:10px; width: 80px; height: 25px;" 
                                                      icon="ui-icon-print" value="Imprimir" 
                                                      actionListener="#{managerOrcamento.gerarRelatorioOrcamento(managerConsultarOrcamento.orcamentoVisualizar.id)}" 
                                                      onclick="this.form.target='_blank'"
                                                      ajax="false" immediate="true" update="@this" process="@this"/>
                                    
                                    <p:commandButton  style="font-size:10px; width: 80px; height: 25px;" 
                                                      onclick="servicoAtoPesquisar.hide()" icon="ui-icon-closethick" 
                                                      process="@this" update="@this" value="Fechar"/>
                                </div>
                            </f:facet>
                        </p:dialog>
                      </h:form>
                </p:tab>
            </p:tabView>
            <!--            </p:panel>-->
        </ui:define>
    </ui:composition>
  
</html>